package com.mall.controller;

import com.mall.entity.CodeNumEntity;
import com.mall.entity.MallCarousel;
import com.mall.entity.PageBean;
import com.mall.entity.ResponseMessage;
import com.mall.service.MallCarouselService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/mallCarousel")
public class MallCarouselController {

    private MallCarouselService mallCarouselService;

    @Autowired
    public MallCarouselController(MallCarouselService mallCarouselService){
        this.mallCarouselService = mallCarouselService;
    }

    /**
     * 根据carouselId查询轮播图数据
     * @param carouselId
     * @return
     */
    @GetMapping("/{carouselId}")
    public ResponseMessage getMallCarouselById(@PathVariable String carouselId){
        if("".equals(carouselId) || carouselId ==null){
            return new ResponseMessage(CodeNumEntity.FAIL.getCode(),"未传入数据");
        }
        Map<String, Object> map = new HashMap<>();
        MallCarousel mallCarousel = mallCarouselService.getMallCarouselById(carouselId);
        if(mallCarousel == null){
            return new ResponseMessage(CodeNumEntity.DATABASE_ERROR.getCode(),CodeNumEntity.DATABASE_ERROR.getMessage());
        }
        map.put("message",mallCarousel);
        return new ResponseMessage(CodeNumEntity.SUCCESS.getCode(), CodeNumEntity.SUCCESS.getMessage(),map);
    }

    @GetMapping("/{currentPage}/{pageSize}")
    public ResponseMessage getMallCarouselList(@PathVariable Integer currentPage, @PathVariable Integer pageSize){
        if(currentPage ==null || pageSize ==null){
            return new ResponseMessage(CodeNumEntity.FAIL.getCode(),"未传入数据");
        }
        Map<String, Object> map = new HashMap<>();
        PageBean pageBean = mallCarouselService.getMallCarouselList(currentPage,pageSize);
        if(pageBean == null){
            return new ResponseMessage(CodeNumEntity.DATABASE_ERROR.getCode(), CodeNumEntity.DATABASE_ERROR.getMessage());
        }
        map.put("message", pageBean);
        return new ResponseMessage(CodeNumEntity.SUCCESS.getCode(), CodeNumEntity.SUCCESS.getMessage(), map);
    }

    @DeleteMapping("/{carouselId}")
    public ResponseMessage deleteMallCarouselById(@PathVariable String carouselId){
        if("".equals(carouselId) || carouselId == null){
            return new ResponseMessage(CodeNumEntity.FAIL.getCode(),"未传入数据");
        }
        Map<String, Object> map = new HashMap<>();
        int result = mallCarouselService.deleteMallCarouselById(carouselId);
        if(result == 0){
            return new ResponseMessage(CodeNumEntity.DATABASE_ERROR.getCode(), CodeNumEntity.DATABASE_ERROR.getMessage());
        }
        map.put("message", result);
        return new ResponseMessage(CodeNumEntity.SUCCESS.getCode(), CodeNumEntity.SUCCESS.getMessage(), map);
    }

    /**
     * 新增一条轮播图数据
     * @param mallCarousel
     * @return
     */
    @PostMapping("/addMallCarousel")
    public ResponseMessage addMallCarousel(@RequestBody MallCarousel mallCarousel){
        if(mallCarousel == null){
            return new ResponseMessage(CodeNumEntity.FAIL.getCode(),"未传入数据");
        }
        Map<String,Object> map = new HashMap<>();
        int result = mallCarouselService.addMallCarousel(mallCarousel);
        if(result == 0){
            return new ResponseMessage(CodeNumEntity.DATABASE_ERROR.getCode(), CodeNumEntity.DATABASE_ERROR.getMessage());
        }
        map.put("message", result);
        return new ResponseMessage(CodeNumEntity.SUCCESS.getCode(), CodeNumEntity.SUCCESS.getMessage(), map);
    }

    /**
     * 更新数据(逻辑删除轮播图也是调用该方法)
     * @param mallCarousel
     * @return
     */
    @PutMapping("")
    public ResponseMessage updateMallCarousel(@RequestBody MallCarousel mallCarousel){
        if(mallCarousel == null){
            return new ResponseMessage(CodeNumEntity.FAIL.getCode(),"未传入数据");
        }
        Map<String,Object> map = new HashMap<>();
        int result = mallCarouselService.updateMallCarousel(mallCarousel);
        if(result == 0){
            return new ResponseMessage(CodeNumEntity.DATABASE_ERROR.getCode(), CodeNumEntity.DATABASE_ERROR.getMessage());
        }
        map.put("message", result);
        return new ResponseMessage(CodeNumEntity.SUCCESS.getCode(), CodeNumEntity.SUCCESS.getMessage(), map);
    }

    /**
     * 逻辑删除轮播图(更新isDelete)
     * @param mallCarousel
     * @return
     */
    @PutMapping("/logicalDeleteCarousel")
    public ResponseMessage logicalDeleteCarousel(@RequestBody MallCarousel mallCarousel){
        if(mallCarousel == null){
            return new ResponseMessage(CodeNumEntity.FAIL.getCode(),"未传入数据");
        }
        mallCarousel.setIsDelete(1);
        Map<String,Object> map = new HashMap<>();
        int result = mallCarouselService.updateMallCarousel(mallCarousel);
        if(result == 0){
            return new ResponseMessage(CodeNumEntity.DATABASE_ERROR.getCode(), CodeNumEntity.DATABASE_ERROR.getMessage());
        }
        map.put("message", result);
        return new ResponseMessage(CodeNumEntity.SUCCESS.getCode(), CodeNumEntity.SUCCESS.getMessage(), map);
    }

    /**
     * 接收数组，数组中有多个MallCarousel对象，然后根据carouselId逻辑删除轮播图
     * @param mallCarouselArrayList
     * @return
     */
    @PutMapping("/updateListMallCarouselById")
    public ResponseMessage updateListMallCarouselById(@RequestBody ArrayList<MallCarousel> mallCarouselArrayList){
        if(mallCarouselArrayList.isEmpty()){
            return new ResponseMessage(CodeNumEntity.FAIL.getCode(),"未传入数据");
        }
        for (MallCarousel mallCarousel: mallCarouselArrayList) {
            mallCarousel.setIsDelete(1);
        }
        ArrayList<String> listCarouselId = new ArrayList<>();
        for (MallCarousel mallCarousel : mallCarouselArrayList) {
            String carouselId = mallCarousel.getCarouselId();
            listCarouselId.add(carouselId);
        }
        Map<String,Object> map = new HashMap<>();
        int result = mallCarouselService.updateListMallCarouselById(listCarouselId);
        if(result == 0){
            return new ResponseMessage(CodeNumEntity.DATABASE_ERROR.getCode(), CodeNumEntity.DATABASE_ERROR.getMessage());
        }
        map.put("message", result);
        return new ResponseMessage(CodeNumEntity.SUCCESS.getCode(), CodeNumEntity.SUCCESS.getMessage(), map);
    }

}
